﻿//	Copyright (c) 2013 Jonathan Loe
//
//	MIT license (http://en.wikipedia.org/wiki/MIT_License)
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights 
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
//	of the Software, and to permit persons to whom the Software is furnished to do so, 
//	subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in all 
//	copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//	INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
//	PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//	FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
//	ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Ordinaire.Data;
using Ordinaire.Db;

namespace Ordinaire
{
    public class JunctionDataManager<TOContext> : DataManager<TOContext>
        where TOContext : OContext
    {
        /// <summary>
        /// Returns a single data matches the specified ids: A and B.
        /// </summary>
        /// <typeparam name="TData">type of junction data</typeparam>
        /// <typeparam name="TDataA">type of data A</typeparam>
        /// <typeparam name="TDataB">type of data B</typeparam>
        /// <typeparam name="TId">type of data unique identifier</typeparam>
        /// <typeparam name="TWho">type of data IAudit</typeparam>
        /// <param name="idA">id of data A</param>
        /// <param name="idB">id of data B</param>
        /// <returns>the data in TData</returns>
        public TData GetData<TData, TDataA, TDataB, TId, TWho>(TId idA, TId idB)
            where TData  : JunctionData<TDataA, TDataB, TId, TWho>
            where TDataA : Data<TId, TWho>
            where TDataB : Data<TId, TWho>
        {
            return Context
                .GetTable<TData>()
                .Where(d => d.IdA.Equals(idA) && d.IdB.Equals(idB))
                .SingleOrDefault();
        }

        /// <summary>
        /// Returns the data B matches with the specified id A as dictionary of name and data B.
        /// </summary>
        /// <typeparam name="TData">type of junction data</typeparam>
        /// <typeparam name="TDataA">type of data A</typeparam>
        /// <typeparam name="TDataB">type of data B</typeparam>
        /// <typeparam name="TId">type of data unique identifier</typeparam>
        /// <typeparam name="TWho">type of data IAudit</typeparam>
        /// <param name="id">id of data A</param>
        /// <returns>a dictionary of name and data B matches with the specified id A</returns>
        public Dictionary<string, TDataB> GetDataBByIdAAsDictionary<TData, TDataA, TDataB, TId, TWho>(TId id)
            where TData  : JunctionData<TDataA, TDataB, TId, TWho>
            where TDataA : Data<TId, TWho>
            where TDataB : Data<TId, TWho>
        {
            return Context
                .GetTable<TData>()
                .Where(d => d.IdA.Equals(id))
                .ToDictionary(d => (d.Name == null) ? String.Empty : d.Name, d => d.DataB);
        }
    }
}
